import { loginAPI } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: () => {
    return {

      token: getToken() || ''
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    // 调用接口
    async doLogin(store, { username, password }) {
      const res = await loginAPI({ username, password })
      // 提交mutation
      store.commit('setToken', res.data.token)
    }

  }
}
